import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import * as moment from 'moment';

import { SERVER_API_URL } from 'app/app.constants';
import { createRequestOption } from 'app/shared/util/request-util';
import { IOrder } from 'app/shared/model/order.model';

type EntityResponseType = HttpResponse<IOrder>;
type EntityArrayResponseType = HttpResponse<IOrder[]>;

@Injectable({ providedIn: 'root' })
export class OrderService {
  public resourceUrl = SERVER_API_URL + 'api/orders';

  constructor(protected http: HttpClient) {}

  create(order: IOrder): Observable<EntityResponseType> {
    const copy = this.convertDateFromClient(order);
    return this.http
      .post<IOrder>(this.resourceUrl, copy, { observe: 'response' })
      .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
  }

  update(order: IOrder): Observable<EntityResponseType> {
    const copy = this.convertDateFromClient(order);
    return this.http
      .put<IOrder>(this.resourceUrl, copy, { observe: 'response' })
      .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
  }

  find(id: number): Observable<EntityResponseType> {
    return this.http
      .get<IOrder>(`${this.resourceUrl}/${id}`, { observe: 'response' })
      .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
  }

  query(req?: any): Observable<EntityArrayResponseType> {
    const options = createRequestOption(req);
    return this.http
      .get<IOrder[]>(this.resourceUrl, { params: options, observe: 'response' })
      .pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
  }

  delete(id: number): Observable<HttpResponse<{}>> {
    return this.http.delete(`${this.resourceUrl}/${id}`, { observe: 'response' });
  }

  protected convertDateFromClient(order: IOrder): IOrder {
    const copy: IOrder = Object.assign({}, order, {
      orderTime: order.orderTime && order.orderTime.isValid() ? order.orderTime.toJSON() : undefined,
      cancelTime: order.cancelTime && order.cancelTime.isValid() ? order.cancelTime.toJSON() : undefined,
      dealTime: order.dealTime && order.dealTime.isValid() ? order.dealTime.toJSON() : undefined,
      sendTime: order.sendTime && order.sendTime.isValid() ? order.sendTime.toJSON() : undefined,
      marginTime: order.marginTime && order.marginTime.isValid() ? order.marginTime.toJSON() : undefined,
      payTime: order.payTime && order.payTime.isValid() ? order.payTime.toJSON() : undefined,
      checkedTime: order.checkedTime && order.checkedTime.isValid() ? order.checkedTime.toJSON() : undefined,
    });
    return copy;
  }

  protected convertDateFromServer(res: EntityResponseType): EntityResponseType {
    if (res.body) {
      res.body.orderTime = res.body.orderTime ? moment(res.body.orderTime) : undefined;
      res.body.cancelTime = res.body.cancelTime ? moment(res.body.cancelTime) : undefined;
      res.body.dealTime = res.body.dealTime ? moment(res.body.dealTime) : undefined;
      res.body.sendTime = res.body.sendTime ? moment(res.body.sendTime) : undefined;
      res.body.marginTime = res.body.marginTime ? moment(res.body.marginTime) : undefined;
      res.body.payTime = res.body.payTime ? moment(res.body.payTime) : undefined;
      res.body.checkedTime = res.body.checkedTime ? moment(res.body.checkedTime) : undefined;
    }
    return res;
  }

  protected convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
    if (res.body) {
      res.body.forEach((order: IOrder) => {
        order.orderTime = order.orderTime ? moment(order.orderTime) : undefined;
        order.cancelTime = order.cancelTime ? moment(order.cancelTime) : undefined;
        order.dealTime = order.dealTime ? moment(order.dealTime) : undefined;
        order.sendTime = order.sendTime ? moment(order.sendTime) : undefined;
        order.marginTime = order.marginTime ? moment(order.marginTime) : undefined;
        order.payTime = order.payTime ? moment(order.payTime) : undefined;
        order.checkedTime = order.checkedTime ? moment(order.checkedTime) : undefined;
      });
    }
    return res;
  }
}
